package com.wenjie.springcloud.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

//  自定义全局过滤器
@Component
public class MyLogGateWayFilter implements GlobalFilter, Ordered {      //如果想要自定义的话，这两个接口都需要进行实现

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("********欢迎进入************");
        String uname = exchange.getRequest().getQueryParams().getFirst("uname");        //获取请求参数中是否有uname属性，没有设置的话，返回值是null
        if (uname==null){
            System.out.println("********用户名null，为非法用户*************");
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);    //设置返回之后http的状态码
            return exchange.getResponse().setComplete();        //          返回
        }
        return chain.filter(exchange);                      // 进行下一条过滤链
    }


//    加载过滤器的顺序，数字越小，优先级越高

    @Override
    public int getOrder() {
        return 0;
    }
}
